﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;
namespace binary
{
    public static class DbfMemo
    {

        public static string getText(string fileName, int blockNumber, int codePage)
        {
            using (BinaryReader stream = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read)))
            {
                byte[] blockSize = new byte[4];
                stream.BaseStream.Seek(blockNumber * 64, SeekOrigin.Begin);
                stream.BaseStream.Seek(4, SeekOrigin.Current); // advance type
                stream.Read(blockSize, 0, 4);
                int memoSize = BitConverter.ToInt32(blockSize.Reverse().ToArray(), 0);
                if(memoSize>10000)
                    throw new Exception(String.Format("Zbyt duża liczba znaków w definicji pola memo: {0}",memoSize));
                byte[] bytes = new byte[memoSize];
                stream.Read(bytes, 0, memoSize);
                string output = Encoding.GetEncoding(codePage).GetString(bytes);
                return output;
            }
        }


    }
}
